#include <stdio.h>
#include <stdlib.h>

int quick_sort( int *data, int low, int high);
int partion(int *data, int low, int high);


int main(int argc, const char * argv[]){

    int data[] = {1, 5, 7, 8, 3, 5, 9, 4, 1, 0};
    for (int i = 0; i < (sizeof(data) / sizeof(int)); i++)
	printf("%d ", data[i]);
    puts("");

    quick_sort(data, 0, sizeof(data) / sizeof(int) - 1);

    for (int i = 0; i < (sizeof(data) / sizeof(int)); i++)
	printf("%d ", data[i]);
    puts("");

    return 0;
}

int partion(int *data, int low, int high){
    int temp = data[low];

    while (low < high) {
	while (low < high && temp <= data[high]) {
	    high--;
	}
	data[low] = data[high];

	while (low < high && temp >= data[low]) {
	    low++;
	}
	data[high] = data[low];
    }

    data[low] = temp;

    return low;
}

int quick_sort( int *data, int low, int high) {
    int t;
    
    if (data == NULL)
	return -1;

    if (low >= high)
	return 0;

    t = partion(data, low, high);
    quick_sort(data, low, t-1);
    quick_sort(data, t+1, high);

    return 0;
}
